﻿using GalaSoft.MvvmLight.Messaging;
using SPI_TOOLS.CustomControl;
using SPI_TOOLS.Models;
using SPI_TOOLS.ViewModels;
using System.Collections.ObjectModel;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace SPI_TOOLS
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        MainViewModel MainViewModel = new MainViewModel();

        private ProgressWindow? ProgressWindow;
        public MainWindow()
        {
            InitializeComponent();
            this.DataContext = MainViewModel;
            Messenger.Default.Register<ProgressMsg>(this, ProgressMsg.Progress_Msg_Key, new Action<ProgressMsg>((msg) =>
            {
                Application.Current.Dispatcher.Invoke(() =>
                {
                    switch (msg.Cmd)
                    {
                        case ProgressMsgType.Start:
                            if (ProgressWindow == null)
                            {
                                ProgressWindow = new ProgressWindow(this);
                            }
                            if (ProgressWindow.IsActive) return;
                            ProgressWindow.ProgressMsg.AllJobCount = msg.AllJobCount;
                            ProgressWindow.ProgressMsg.CurrentJobCount = msg.CurrentJobCount;
                            ProgressWindow.Title = msg.Title;
                            ProgressWindow.ProgressMsg.JobName = msg.JobName;
                            ProgressWindow.Show();
                            this.IsEnabled = false;
                            break;
                        case ProgressMsgType.go:
                            if (ProgressWindow == null) return;
                            ProgressWindow.ProgressMsg.AllJobCount = msg.AllJobCount;
                            ProgressWindow.ProgressMsg.CurrentJobCount = msg.CurrentJobCount;
                            ProgressWindow.ProgressMsg.JobName = msg.JobName;
                            break;
                        case ProgressMsgType.end:
                            if (ProgressWindow == null) return;
                            ProgressWindow.ProgressMsg.AllJobCount = msg.AllJobCount;
                            ProgressWindow.ProgressMsg.CurrentJobCount = msg.CurrentJobCount;
                            ProgressWindow.ProgressMsg.JobName = msg.JobName;
                            ProgressWindow.Close();
                            ProgressWindow = null;
                            this.IsEnabled = true;
                            break;
                    }
                });
            }));
        }
    }
    public enum ProgressMsgType
    {
        Start,
        go,
        end
    }
    public class ProgressMsg:ModelNotifyBase
    {
        public static string Progress_Msg_Key = "progress_msg";
        public ProgressMsgType Cmd { get; set; }
        private string title = "进度条";
        public string Title 
        {
            get { return title; }
            set
            {
                title = value;
                RaisePropertyChanged(nameof(Title));
            }
        }
        private string jobName = "任务";
        public string JobName
        {
            get { return jobName; }
            set
            {
                jobName = value;
                RaisePropertyChanged(nameof(JobName));
                UpdateNotifyText();
            }
        }
        private int allJobCount = 0;
        public int AllJobCount {
            get { return allJobCount; } 
            set
            { 
                allJobCount = value;
                RaisePropertyChanged(nameof(AllJobCount));
                UpdateNotifyText();
            }
        }
        private int currentJobCount = 0;
        public int CurrentJobCount
        {
            get { return currentJobCount; } 
            set
            {
                currentJobCount = value;
                RaisePropertyChanged(nameof(CurrentJobCount));
                UpdateNotifyText();
            }
        }
        private string notifyTextx = "任务进行中...";
        public string NotifyTextX
        {
            get { return notifyTextx; }
            set
            {
                notifyTextx = value;
                RaisePropertyChanged(nameof(NotifyTextX));
            }
        }
        public void UpdateNotifyText()
        {
            NotifyTextX = $"{JobName}:{CurrentJobCount}/{AllJobCount}";
        }
    }
}